﻿

//声明了个控制权名称：common.views.disputeInformations.index
(function () {
    appModule.controller('common.views.disputeInformationHandles.lvXingXieYi', [
      '$scope', '$uibModal', '$stateParams', 'uiGridConstants', 'abp.services.app.disputeInformation',
        function ($scope, $uibModal, $stateParams, uiGridConstants, disputeInformationService) {
            //$stateParams 获取的是浏览器后面跟的参数
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                //这里应该是当页面加载完毕后，进行信息的初始化
                //实际会去调用 icheck、select2等js的初始化插件。来渲染页面
                App.initAjax();
            });
            //告知页面信息已经下载完毕
            vm.loading = false;
            //默认是关闭高级按钮。我们这里用不到。参考
            //  vm.advancedFiltersAreShown = false;
            //获取传递的参数，判断模糊查询字段，是否为空
            vm.filterText = $stateParams.filterText || '';
            //获取Session中的userId
            //   vm.currentUserId = abp.session.userId;
            //制作权限组，进行页面权限的判断 
            vm.permissions = {
                perform: abp.auth.hasPermission("Pages.PerformanceAgreement.Perform"),
                unfulfilled: abp.auth.hasPermission("Pages.PerformanceAgreement.Unfulfilled"),
                detail: abp.auth.hasPermission("Pages.PerformanceAgreement.Detail"),
                viewWorkFlowProcess: abp.auth.hasPermission("Pages.PerformanceAgreement.ViewWorkFlowProcess"),
                duBanList: abp.auth.hasPermission("Pages.DisputeInformation.DuBanList")
            };
            //请求参数，默认用于分页
            vm.requestParams = {
                permission: '',
                role: '',
                skipCount: 0,
                //这里是个常量文件，如果你没有找到这个常量文件，那么就手动改成10吧
                maxResultCount: app.consts.grid.defaultPageSize,
                sorting: null
            };

            //配置表格信息

            vm.disputeInformationGridOptions = {
                enableHorizontalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                enableVerticalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                //这里是个常量文件，如果你没有找到这个常量文件，那么就手动改成[10, 20, 50, 100]吧
                paginationPageSizes: app.consts.grid.defaultPageSizes,
                paginationPageSize: app.consts.grid.defaultPageSize,
                useExternalPagination: true,
                useExternalSorting: true,
                appScopeProvider: vm,
                rowTemplate: '<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name" class="ui-grid-cell" ng-class="{ \'ui-grid-row-header-cell\': col.isRowHeader, \'text-muted\': !row.entity.isActive }"  ui-grid-cell></div>',

                columnDefs: [
                       //id不展示信息
                       {
                           name: app.localize('TownID'),
                           field: 'townTitle',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('AreaID'),
                           field: 'areaTitle',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('VillageID'),
                           field: 'villageTitle',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('UploadName'),
                           field: 'uploadName',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('DisputeTypeID'),
                           field: 'disputeTypeTitle',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('EventDesc'),
                           field: 'eventDesc',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('SourceID'),
                           field: 'sourceTitle',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('EventAddress'),
                           field: 'eventAddress',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('Party'),
                           field: 'party',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           name: app.localize('InvolveNum'),
                           field: 'involveNum',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 20
                       },
                       {
                           name: '登记时间',
                           field: 'creationTime',
                           //  enableSorting: false,
                           cellFilter: 'momentFormat: \'YYYY-MM-DD HH:mm:ss\'',
                           minWidth: 200
                       },


                       {
                           name: app.localize('Status'),
                           field: 'currentProcessName',
                           //  enableSorting: false,
                           //cellFilter: 'momentFormat: \'L\'',
                           minWidth: 100
                       },
                       {
                           //这里是在处理多语言本地化的问题，如果你这里报错。那么手动修改名字吧。
                           name: app.localize('Actions'),
                           enableSorting: false,
                           width: 120,
                           cellTemplate:
                               '<div class=\"ui-grid-cell-contents\">' +
                               '  <div class="btn-group dropdown" uib-dropdown="" dropdown-append-to-body>' + '    <button class="btn btn-xs btn-primary blue" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false"><i class="fa fa-cog"></i> ' + app.localize('Actions') + ' <span class="caret"></span></button>' +
                               '    <ul uib-dropdown-menu>' +
                               '      <li><a ng-if="grid.appScope.permissions.perform" ng-click="grid.appScope.perform(row.entity)">' + app.localize('Perform') + '</a></li>' +
                               '      <li><a ng-if="grid.appScope.permissions.unfulfilled" ng-click="grid.appScope.unfulfilled(row.entity)">' + app.localize('Unfulfilled') + '</a></li>' +
                               '      <li><a ng-if="grid.appScope.permissions.detail" ng-click="grid.appScope.detail(row.entity)">' + app.localize('Detail') + '</a></li>' +
                               '      <li><a ng-if="grid.appScope.permissions.viewWorkFlowProcess" ng-click="grid.appScope.viewWorkFlowProcess(row.entity)">' + app.localize('ViewWorkFlowProcess') + '</a></li>' +
                               '      <li><a ng-if="grid.appScope.permissions.duBanList" ng-click="grid.appScope.duBanList(row.entity)">' + app.localize("DuBanList") + '</a></li>' +
                               '    </ul>' +
                               '  </div>' +
                               '</div>'
                       },
                ],
                // ui-grid进行API注册渲染数据。
                onRegisterApi: function (gridApi) {
                    $scope.gridApi = gridApi;
                    $scope.gridApi.core.on.sortChanged($scope, function (grid, sortColumns) {
                        if (!sortColumns.length || !sortColumns[0].field) {
                            vm.requestParams.sorting = null;
                        } else {
                            vm.requestParams.sorting = sortColumns[0].field + ' ' + sortColumns[0].sort.direction;
                        }
                        vm.getPageddisputeInformations();
                    });
                    //配置UI-grid的 API参数
                    gridApi.pagination.on.paginationChanged($scope, function (pageNumber, pageSize) {
                        vm.requestParams.skipCount = (pageNumber - 1) * pageSize;
                        vm.requestParams.maxResultCount = pageSize;
                        //执行查询语句
                        vm.getPageddisputeInformations();
                    });
                },
                data: []
            };

            //声明查询方法
            vm.getPageddisputeInformations = function () {
                vm.loading = true;
                disputeInformationService.getPagedDisputeInformationsAsync($.extend({ filter: vm.filterText, Status: 4 }, vm.requestParams))
                    .then(function (result) {
                        vm.disputeInformationGridOptions.totalItems = result.data.totalCount;
                        //       console.log(result.data.items);                        
                        vm.disputeInformationGridOptions.data = result.data.items;
                    }).finally(function () {
                        vm.loading = false;
                    });
            };

            //导出为excel文档
            vm.exportToExcel = function () {
                disputeInformationService.getDisputeInformationToExcel({})
                      .then(function (result) {
                          app.downloadTempFile(result.data);
                      });
            };

            //触发履行方法
            vm.perform = function (disputeInformation) {
                vm.saving = true;
                vm.disputeInformation = disputeInformation;
                vm.disputeInformation.processContent = '进行纠纷事件履行';
                disputeInformationService.createOrUpdateDisputeInformationAsync({ disputeInformationEditDto: vm.disputeInformation }).then(function () {
                    vm.getPageddisputeInformations();
                }).finally(function () {
                    vm.saving = false;
                });
            };

            //触发未履行方法
            vm.unfulfilled = function (disputeInformation) {
                vm.saving = true;
                vm.disputeInformation = disputeInformation;
                vm.disputeInformation.processContent = '进行纠纷事件未履行';
                disputeInformationService.createOrUpdateDisputeInformationAsync({ disputeInformationEditDto: vm.disputeInformation }).then(function () {
                    vm.getPageddisputeInformations();
                }).finally(function () {
                    vm.saving = false;
                });
            };

            //详情功能
            vm.detail = function (disputeInformation) {
                var templateUrl = '~/App/common/views/disputeInformations/detail.cshtml';
                var controller = 'common.views.disputeInformationHandles.detail as vm';
                openCreateOrEditDisputeInformationModal(disputeInformation.id, disputeInformation, templateUrl, controller);
            }

            //查看流程轨迹
            vm.viewWorkFlowProcess = function (disputeInformation) {
                var templateUrl = '~/App/common/views/disputeInformations/workFlowProcess.cshtml';
                var controller = 'common.views.disputeInformationHandles.workFlowProcess as vm';
                openCreateOrEditDisputeInformationModal(disputeInformation.id, disputeInformation, templateUrl, controller);
            }

            //查看跟踪督办记录功能
            vm.duBanList = function (disputeInformation) {
                var templateUrl = '~/App/common/views/disputeInformations/duBanList.cshtml';
                var controller = 'common.views.disputeInformationHandles.duBanList as vm';
                openCreateOrEditDisputeInformationModal(disputeInformation.id, disputeInformation, templateUrl, controller);
            }

            //打开模态框，进行创建或者编辑的功能操作
            function openCreateOrEditDisputeInformationModal(disputeInformationId, disputeInformation, templateUrl, controller) {
                var modalInstance = $uibModal.open({
                    size: 'lg',
                    animation: true,
                    templateUrl: templateUrl,
                    controller: controller,
                    backdrop: 'static',
                    resolve: {
                        disputeInformationId: function () {
                            return disputeInformationId;
                        },
                        disputeInformation: function () {
                            return disputeInformation;
                        }
                    }
                });

                modalInstance.result.then(function (result) {
                    vm.getPageddisputeInformations();
                });
            }

            //执行查询方法
            vm.getPageddisputeInformations();
        }
    ]);
})();











